//#ifndef MAC_OS
#include "stdafx.h"
//#endif
#include "CMenuItem.h"
#include "CGame.h"

CMenuItem::CMenuItem(INT16 cmd, int x, int y, CAnimObject* anim, bool isFont, UINT8* aIds, UINT8 fanim)
{
	iCommand = cmd;
	pAnim = anim;
	if(!aIds)
	{
		UINT8 tmp[] = 
		{
			k_Item_Anim_Normal,
			k_Item_Anim_Hover,
			k_Item_Anim_Select,
			k_Item_Anim_Disable,
		};
		aIds = tmp;
	}
	memcpy(pAnims, aIds, k_Item_Anim_Font);
	pAnims[k_Item_Anim_Font] = fanim;
	bUseFontPal = isFont;
	SetPos(x, y);
	SetState(k_Item_Anim_Normal);
	bVisible = true;
}

CMenuItem::~CMenuItem()
{
	SAFE_DEL(pAnim);
}
//bool CMenuItem::HandlePoiter(int px, int py)
//{
//	if(iCurrentAnim == k_Item_Anim_Normal && pAnim->IsInside(px, py))
//	{
//		SetState(k_Item_Anim_Select);
//		return true;
//	}
//	return false;
//}

bool CMenuItem::IsPointerHit(int px, int py)
{
	return iCurrentAnim == k_Item_Anim_Disable || bVisible == false? false : pAnim->IsInside(px, py);
}

void CMenuItem::SetPos(int x, int y)
{
	iX = x;
	iY = y;
	pAnim->SetPos(iX, iY);
}

void CMenuItem::SetEnable(bool enable)
{	
	SetState(enable?k_Item_Anim_Normal:k_Item_Anim_Disable);
}

bool CMenuItem::IsEnable()
{
	return (iCurrentAnim != k_Item_Anim_Disable);
}

void CMenuItem::SetVisible(bool show)
{
	bVisible = show;
}

bool CMenuItem::IsVisible()
{
	return bVisible;
}

void CMenuItem::SetState(int state)
{
	iCurrentAnim = state;
	pAnim->SetAnim(pAnims[bUseFontPal?k_Item_Anim_Font:iCurrentAnim], iCurrentAnim!=k_Item_Anim_Select);
}

void CMenuItem::GetPos(POINT *p)
{
	p->x = iX;
	p->y = iY;
}

UINT8 CMenuItem::GetAnimIndex(int state)
{
	return pAnims[state];
}

INT16 CMenuItem::GetCommand()
{
	return iCommand;
}

void CMenuItem::ModifyStateAnim(int state, UINT8 anim, bool setAnim)
{
	if(state <= k_Item_Anim_Disable)
	{
		if(!bUseFontPal)
			pAnims[state] = anim;
		else
			pAnims[k_Item_Anim_Font] = anim;

		if(setAnim)
			SetState(state);
	}
}

void CMenuItem::ModifyStateAnims(UINT8* anims)
{
	if(anims && !bUseFontPal)
	{
		memcpy(pAnims, anims, k_Item_Anim_Font);
	}
}

void CMenuItem::Move(int dx, int dy)
{
	SetPos(iX + dx, iY + dy);
}

INT16 CMenuItem::Update()
{
	if(bVisible && iCurrentAnim == k_Item_Anim_Select) //item is in select anim
	{
		if(pAnim->IsCurrentAnimStop())
		{
			SetState(k_Item_Anim_Normal);
			return iCommand;
		}
		return k_Item_Command_Wait;
	}
	return k_Item_Command_None;
}

void CMenuItem::Draw(GLESLib *g)
{
	if(bVisible)
	{
		if(bUseFontPal)
			pAnim->_anim_sprite->SetDrawPalette(0, pAnims[iCurrentAnim]);
		pAnim->DrawAnimObject(g);
		pAnim->UpdateAnimObject();
	}
}
